package com.lnking.lnkingplace.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.mapper.NoticeMapper;
import com.lnking.lnkingplace.mapper.PlaceInfoMapper;
import com.lnking.lnkingplace.mapper.ReadStatisticsMapper;
import com.lnking.lnkingplace.mapper.ReadUserMapper;
import com.lnking.lnkingplace.model.dto.notice.NoticeDetailDto;
import com.lnking.lnkingplace.model.dto.read.ReadStatisticsDto;
import com.lnking.lnkingplace.model.entity.Notice;
import com.lnking.lnkingplace.model.entity.PlaceInfo;
import com.lnking.lnkingplace.model.entity.ReadStatistics;
import com.lnking.lnkingplace.model.entity.ReadUser;
import com.lnking.lnkingplace.model.enums.NoticeType;
import com.lnking.lnkingplace.model.enums.ReadType;
import com.lnking.lnkingplace.model.enums.SuggestType;
import com.lnking.lnkingplace.model.request.notice.NoticeAddRequest;
import com.lnking.lnkingplace.model.request.notice.NoticeUpdateRequest;
import com.lnking.lnkingplace.service.INoticeService;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangqt
 * @since 2022-08-19
 */
@Service
public class NoticeServiceImpl extends CustomServiceImpl<NoticeMapper, Notice> implements INoticeService {

    @Resource
    UserUtil userUtil;

    @Resource
    PlaceInfoMapper placeInfoMapper;

    @Resource
    ReadStatisticsMapper readStatisticsMapper;

    @Resource
    ReadUserMapper readUserMapper;

    @Override
    public void add(NoticeAddRequest params) throws Exception {
        Notice entity = new Notice();
        BeanUtils.copyProperties(params, entity);
        entity.setCreateTime(LocalDateTime.now());
        //获取用户信息并写入
        String channel = userUtil.getUser().getChannel();
        String userId = userUtil.getUser().getId();
        String userName = userUtil.getUser().getName();
        entity.setCreatorName(userName);
        entity.setChannel(channel);
        entity.setUserId(userId);

        //判断通知类型 存入消息表
        int count = 0;
        String type = params.getType();
        if(NoticeType.ALL.equals(type)){
            //查询channel下所有场所
            count = placeInfoMapper.queryCountAll(channel);
            insertInfo(entity,count);
        }
        else if(NoticeType.TRADE.equals(type)){
            //查询channel下该行业的场所

            String[] split = params.getTradeType().split(";");
            for (String tradeTyp : split){
                entity.setTradeType(tradeTyp);
                count = placeInfoMapper.queryCountByType(channel,tradeTyp);
                insertInfo(entity,count);
            }
        }
        else if(NoticeType.PRECISE.equals(type)){
            //查询channel下的精准通知
            String[] split = params.getPreciseId().split(";");
            for(String preciseId : split){
                entity.setPreciseId(preciseId);
                count = placeInfoMapper.queryCountByPreciseId(channel,preciseId);
                insertInfo(entity,count);
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertInfo(Notice noticeInfo,Integer count){
        getBaseMapper().insert(noticeInfo);
        ReadStatistics readInfo = new ReadStatistics();
        readInfo.setNoticeId(noticeInfo.getId());
        readInfo.setTotal(count);
        readStatisticsMapper.insert(readInfo);
    }

    @Override
    public void update(NoticeUpdateRequest params) throws Exception {
        Notice entity = new Notice();
        BeanUtils.copyProperties(params, entity);
        getBaseMapper().updateById(entity);
    }

    @Override
    public void read(String id) {
        ReadStatistics info = readStatisticsMapper.getInfoByNoticeId(id);
        info.setReadNum(info.getReadNum()+1);
        readStatisticsMapper.updateById(info);

        //记录已读的用户
        String userId = userUtil.getUser().getId();
        ReadUser readUser = new ReadUser();
        readUser.setNoticeId(id);
        readUser.setUserId(userId);
        readUser.setUserId(userId);
        readUserMapper.insert(readUser);
    }

    @Override
    public ReadStatisticsDto queryReadInfo(String id) {
        ReadStatistics info = readStatisticsMapper.getInfoByNoticeId(id);
        ReadStatisticsDto data = new ReadStatisticsDto();
        data.setTotal(info.getTotal());
        data.setRead(info.getReadNum());
        data.setUnread(info.getTotal()- info.getReadNum());
        return data;
    }

    @Override
    public NoticeDetailDto getOne(String noticeId) {
        Notice notice = this.getById(noticeId);
        NoticeDetailDto info = new NoticeDetailDto();
        BeanUtils.copyProperties(notice,info);
        String userId = userUtil.getUser().getId();

        String id = readUserMapper.getId(userId, noticeId);

        if(StringUtils.isNotBlank(id)){
            info.setIsRead(ReadType.READ);
        }
        else {
            info.setIsRead(ReadType.UNREAD);
        }
        return info;
    }
}
